﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Atomic.Permission;
using Atomic.Permission.Entities;
using Atomic.Common;
using Newtonsoft.Json;

namespace Atomic.Web.Handlers.Sys
{
    /// <summary>
    /// ResourceHandler 的摘要说明
    /// </summary>
    public class ResourceHandler : BaseHandler
    {
        static Dictionary<string, Action<HttpContext>> actions = new Dictionary<string, Action<HttpContext>>();
        protected override Dictionary<string, Action<HttpContext>> Actions
        {
            get { return actions; }
        }

        /// <summary>
        /// 获取资源列表
        /// </summary>
        /// <param name="context"></param>
        public void GetResourceList(HttpContext context)
        {
            int parentID = context.Request["ID"].ToInt32(0);
            IForm form = ContextFactory.Context.GetObject("Form") as IForm;
            List<FormEntity> list = form.GetChildren(parentID);
            context.Response.Write(JsonConvert.SerializeObject(list));
        }

        /// <summary>
        /// 新建/编辑资源
        /// </summary>
        /// <param name="context"></param>
        public void AddResource(HttpContext context)
        {
            IForm form = ContextFactory.Context.GetObject("Form") as IForm;
            FormEntity entity = new FormEntity()
            {
                ID = context.Request["ID"].ToInt32(0),
                PID = context.Request["PID"].ToInt32(0),
                Key = context.Request["Key"],
                Name = context.Request["Name"],
                Url = context.Request["Url"],
                Sort = context.Request["Sort"].ToInt32(0),
                Cls = context.Request["Cls"],
                Enabled = (context.Request["Enabled"] == "1") ? true : false
            };
            if ((entity.ID > 0) ? form.Update(entity, entity.ID) : form.Add(entity))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = form.GetError().Message
                }));
            }
        }

        /// <summary>
        /// 获取资源名称
        /// </summary>
        /// <param name="context"></param>
        public void GetResourceName(HttpContext context)
        {
            int id = context.Request["PID"].ToInt32(0);
            IForm form = ContextFactory.Context.GetObject("Form") as IForm;
            FormEntity frmEntity = form.Get(id);
            if (frmEntity == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(string.Empty));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(frmEntity.Name));
            }
        }

        /// <summary>
        /// 获取资源基本信息
        /// </summary>
        /// <param name="context"></param>
        public void GetResource(HttpContext context)
        {
            int id = context.Request["ID"].ToInt32(0);
            IForm form = ContextFactory.Context.GetObject("Form") as IForm;
            FormEntity frmEntity = form.Get(id);
            if (frmEntity == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(null));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(frmEntity));
            }
        }

        /// <summary>
        /// 删除指定资源
        /// </summary>
        /// <param name="context"></param>
        public void RemoveResource(HttpContext context)
        {
            int id = context.Request["ID"].ToInt32(0);
            IForm form = ContextFactory.Context.GetObject("Form") as IForm;
            if (form.Remove(id))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true,
                    Message = form.GetError().Message
                }));
            }
        }

        /// <summary>
        /// 查询资源（附带资源功能权限）列表
        /// </summary>
        /// <param name="context"></param>
        public void GetResourceWithFunList(HttpContext context)
        {
            int parentID = context.Request["ID"].ToInt32(0);
            IForm form = ContextFactory.Context.GetObject("Form") as IForm;
            IFunPoint fun = ContextFactory.Context.GetObject("FunPoint") as IFunPoint;
            List<FormEntity> list = form.GetChildren(parentID);
            List<FunPointEntity> funList = fun.GetList();
            foreach (var item in list)
            {
                //筛选本窗体，已启用的功能权限
                item.FunPoints = funList.Where(m => m.FormID == item.ID && m.Enabled == true).ToList();
            }
            context.Response.Write(JsonConvert.SerializeObject(list));
        }

        /// <summary>
        /// 新建/修改资源功能
        /// </summary>
        /// <param name="context"></param>
        public void AddResourceFun(HttpContext context)
        {
            int formID = context.Request["FormID"].ToInt32(0);
            IFunPoint fun = ContextFactory.Context.GetObject("FunPoint") as IFunPoint;
            FunPointEntity entity = new FunPointEntity()
            {
                ID = context.Request["ID"].ToInt32(0),
                FormID = formID,
                Key = context.Request["Key"],
                Name = context.Request["Name"],
                Enabled = context.Request["Enabled"] == "1" ? true : false
            };
            if (entity.ID > 0 ? fun.Update(entity, entity.ID) : fun.Add(entity))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = fun.GetError().Message
                }));
            }
        }

        /// <summary>
        /// 获取资源功能列表
        /// </summary>
        /// <param name="context"></param>
        public void GetResourceFunList(HttpContext context)
        {
            int formID = context.Request["FormID"].ToInt32(0);
            IFunPoint fun = ContextFactory.Context.GetObject("FunPoint") as IFunPoint;
            List<FunPointEntity> funList = fun.GetList(m => m.FormID == formID);
            context.Response.Write(JsonConvert.SerializeObject(new
            {
                total = funList.Count,
                rows = funList
            }));
        }

        /// <summary>
        /// 查询资源指定功能基本信息
        /// </summary>
        /// <param name="context"></param>
        public void GetResourceFun(HttpContext context)
        {
            int id = context.Request["ID"].ToInt32(0);
            IFunPoint fun = ContextFactory.Context.GetObject("FunPoint") as IFunPoint;
            FunPointEntity entity = fun.GetList(m => m.ID == id).FirstOrDefault();
            context.Response.Write(JsonConvert.SerializeObject(entity));
        }

        /// <summary>
        /// 删除功能点
        /// </summary>
        /// <param name="context"></param>
        public void RemoveResourceFun(HttpContext context)
        {
            int id = context.Request["ID"].ToInt32(0);
            IFunPoint fun = ContextFactory.Context.GetObject("FunPoint") as IFunPoint;
            if (fun.Remove(id))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = fun.GetError().Message
                }));
            }
        }
    }
}